package com.imooc.miaosha.dao;

import com.imooc.miaosha.domian.MiaoShaOrder;
import com.imooc.miaosha.domian.OrderInfo;
import org.apache.ibatis.annotations.*;

/**
 * 模块描述: <br>
 * ()
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/19 10:58
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Mapper
public interface OrderDao {


    /**
     * 查询当前用户是否秒杀过此商品
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @Select("select id from miaosha_order mo where mo.user_id = #{userId} AND mo.goods_Id = #{goodsId}")
    MiaoShaOrder getMiaoshaOrderByUserIdGoodsId(Long userId, Long goodsId);

    /**
     * 插入一条订单
     *
     * @param orderInfo
     * @return
     */
    @Insert("insert into order_info(user_id, goods_id, goods_name, goods_count, goods_price, order_channel, status, create_date)values("
            + "#{userId}, #{goodsId}, #{goodsName}, #{goodsCount}, #{goodsPrice}, #{orderChannel},#{status},#{createDate} )")
    @SelectKey(keyColumn = "id", keyProperty = "id", resultType = long.class, before = false, statement = "select last_insert_id()")
    long insert(OrderInfo orderInfo);

    /**
     * 插入一条秒杀订单
     *
     * @param miaoShaOrder
     */
    @Insert("insert into miaosha_order (user_id, goods_id, order_id)values(#{userId}, #{goodsId}, #{orderId})")
    void insertMiaoShaOrder(MiaoShaOrder miaoShaOrder);

    /**
     * 根据id查询订单
     *
     * @param orderId
     * @return
     */
    @Select("select * from order_info where id = #{orderId}")
    OrderInfo getOrderById(@Param("orderId") long orderId);

    /**
     * 删除订单信息
     */
    @Delete("delete from order_info")
    void deleteOrders();

    /**
     * 删除秒杀订单信息
     */
    @Delete("delete from miaosha_order")
    void deleteMiaoshaOrders();
}
